const tree = [
  {
    id: 1,
    parent_id: 0,
    name: '四川省',
    children: [
      {
        id: 5,
        parent_id: 1,
        name: '成都市',
        children: [
          {
            id: 6,
            parent_id: 5,
            name: '锦江区',
            children: [
              { id: 7, parent_id: 6, name: '九眼桥' },
              { id: 8, parent_id: 6, name: '兰桂坊' },
            ],
          },
        ],
      },
    ],
  },
  {
    id: 2,
    parent_id: 0,
    name: '广东省',
    children: [
      {
        id: 9,
        parent_id: 2,
        name: '东莞市',
        children: [{ id: 10, parent_id: 9, name: '长安镇' }],
      },
    ],
  },
  {
    id: 3,
    parent_id: 0,
    name: '江西省',
    children: [{ id: 11, parent_id: 3, name: '南昌市' }],
  },
]

let result = []

function flatTree(nodes, parentId) {
  if (!nodes || nodes.length === 0) return []
  nodes.forEach(node => {
    result.push({
      id: node.id,
      parentId: parentId,
      name: node.name,
    })
    return flatTree(node.children, node.id)
  })
}

flatTree(tree, 0)

console.log(result)
